import grails.test.*

class TaskTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
        
        mockDomain(Project)
        mockForConstraintsTests(Project)
        
        mockDomain(Task)
        mockForConstraintsTests(Task)
        
        def projects = [
            new Project(name:"SDQI", description: "An Agile Java Project.", startDate: new Date(), endDate: new Date() + 30, unitPerPhase: 5),
            new Project(name:"WAE", description: "An Agile Ruby Project.", startDate: new Date() - 30, endDate: new Date() + 30, unitPerPhase: 10),
            new Project(name:"DSA", description: "An Optional Project.", startDate: new Date() + 10, endDate: new Date() + 20, unitPerPhase: 3)
        ]
        projects*.save(flush:true)
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testCreateTask() {
        def psp0 = new Task()
        assertFalse psp0.validate()

        psp0.name = "PSP0"
        psp0.description = "SD Real Number"
        psp0.startDate = new Date(2010, 8, 1)
        psp0.endPlanDate = new Date(2010, 8, 6)
        psp0.endDate = new Date(2010, 8, 7)
        psp0.hours = 4
        assertFalse psp0.validate()

        def SDQI = Project.findByName("SDQI")
        psp0.project = SDQI
        assertNotNull psp0.save()
    }


    void testFindTasks() {
        def SDQI = Project.findByName("SDQI")
        def WAE = Project.findByName("WAE")

        def tasks = [
            new Task(project: SDQI, name:"PSP0", description: "SD Real Number", startDate: new Date(2010, 8, 1), endPlanDate: new Date(2010, 8, 6), endDate: new Date(2010, 8, 7), hours: 5),
            new Task(project: SDQI, name:"PSP0.1", description: "LOC Counter", startDate: new Date(2010, 8, 1), endPlanDate: new Date(2010, 8, 6), endDate: new Date(2010, 8, 7), hours: 5),
            new Task(project: WAE,  name:"PS1", description: "Basics", startDate: new Date(2010, 8, 1), endPlanDate: new Date(2010, 8, 6), endDate: new Date(2010, 8, 7), hours: 5),
        ]
        tasks*.save(flush:true)


	assertEquals 3, Task.list().size()
        assertEquals 2, Task.findAllByProject(SDQI).size()
        assertEquals 1, Task.findAllByProject(WAE).size()
        
        assertNotNull Task.findByName("PSP0")
        assertNull Task.findByName("PSP3")
    }
}
